package com.example.demo.bean;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.example.demo.constant.ScheduleConstants;
import com.example.demo.utils.CronUtils;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;

@Data
@TableName("sys_job")
@Accessors(chain = true)
public class SysJob extends BaseDomain {

    @TableId
    private Long jobId;
    private String jobName;
    private String jobGroup;
    private String invokeTarget;
    private String cronExp;
    private Integer misfirePolicy = ScheduleConstants.MISFIRE_DEFAULT;
    private Integer concurrent;
    private Integer status;
    @TableField(exist = false)
    private Date nextValidTime;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    public Date getNextValidTime(){
        if(StringUtils.isNotEmpty(cronExp)){
            return CronUtils.getNextExecution(cronExp);
        }
        return null;
    }
}
